package com.chezhujie.www.core;

import com.alibaba.fastjson.JSONObject;
import com.chezhujie.www.domain.Czjrecommendpic;
import com.chezhujie.www.domain.vo.AccessOpenId;
import com.chezhujie.www.domain.vo.AccessToken;
import com.chezhujie.www.domain.vo.WeixinInfo;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 汤伟杰 on 2017/5/8.
 */
public class StaticMap {
//    public static final String appid = "wx5e3453d525417756";//测试环境
    public static String appid = PropertiesData.getProperties("appid");
//    public static final String appid = "wx73af8dec75fd96bd";//正式环境
//    public static final String secret = "353ccd9dc8553cd4c28a5d9057f75542";//测试环境
    public static final String secret = PropertiesData.getProperties("secret");//测试环境
//    public static final String secret = "9b7d436be3a586c7d991e1ef42e4f294";//正式环境
public static final String payKey = PropertiesData.getProperties("payKey");//测试环境
public static final String mch_id = PropertiesData.getProperties("mch_id");//测试环境
public static final String MapKey = PropertiesData.getProperties("MapKey");//获取地址信息
public static final String weatherKey = PropertiesData.getProperties("weather");//获取天气预报
public static final String calendarKey = PropertiesData.getProperties("calendar");//获取万年历信息
    public static final String version = "1.0.1";
//    public static final String addressName = PropertiesData.getProperties("addressName");//获取域名
    private static AccessToken accessToken;//微信token
    private static AccessToken accessToken1;//微信token支付测试
    private static Map<String,String> accessOpenIdMap = new HashMap<String, String>();//微信信息
    private static Map<String,String> weixinInfoJson = new HashMap<String, String>();//微信信息json存储
    private static Map<String,String> recommendpicJson = new HashMap<String, String>();//8小时后弹出广告框
    private static boolean onoff_recommendpic = true;//8小时后弹出广告框 只运行一次，从数据库更新数据

    public static Map<String, String> getWeixinInfoJson() {
        return weixinInfoJson;
    }
    public static String getWeixinInfoJson(String openId) {
        return weixinInfoJson.get(openId);
    }
    public static WeixinInfo weixinInfoJsonTo(String openId) {
        if(weixinInfoJson.get(openId)!=null){
            return JSONObject.parseObject(weixinInfoJson.get(openId), WeixinInfo.class);
        }
        return null;
    }

    public static void setWeixinInfoJson(Map<String, String> weixinInfoJson) {
        StaticMap.weixinInfoJson = weixinInfoJson;
    }

    public static Map<String, String> getAccessOpenIdMap() {
        return accessOpenIdMap;
    }
    public static AccessOpenId getAccessOpenIdMap(String code) {
        if(accessOpenIdMap.get(code)==null){
            return null;
        }
        return JSONObject.parseObject(accessOpenIdMap.get(code),AccessOpenId.class);
    }


    public static AccessToken getAccessToken() {
        return accessToken;
    }

    public static void setAccessToken(AccessToken accessToken) {
        StaticMap.accessToken = accessToken;
    }

    public static Map<String, String> getRecommendpicJson(JdbcTemplate jdbcTemplate) {
        setDataToRecommendpicJson(jdbcTemplate);
        return recommendpicJson;
    }
    public static Czjrecommendpic getRecommendpicJson(String key,JdbcTemplate jdbcTemplate) {
        String json = recommendpicJson.get(key);
        setDataToRecommendpicJson(jdbcTemplate);
        if(json==null){
            return null;
        }
        Czjrecommendpic recommendpic = JSONObject.parseObject(json, Czjrecommendpic.class);
        return recommendpic;
    }

    public static void setDataToRecommendpicJson(JdbcTemplate jdbcTemplate){
        if(onoff_recommendpic){
            List<Map<String,Object>> listmap = new ArrayList<Map<String,Object>>();jdbcTemplate.queryForList("select * from czj_xy_recommendpic");
            for (Map<String, Object> map : listmap) {
                recommendpicJson.put(String.valueOf(map.get("recommendpicId")), JSONObject.toJSONString(map));
            }
            onoff_recommendpic = false;
        }
    }

    public static AccessToken getAccessToken1() {
        return accessToken1;
    }

    public static void setAccessToken1(AccessToken accessToken1) {
        StaticMap.accessToken1 = accessToken1;
    }


}

